//
//  CourseWareController.swift
//  Amicool
//
//  Created by Mac  on 2017/6/13.
//  Copyright © 2017年 Mac . All rights reserved.
//

import UIKit

class CourseWareController: UITableViewController {
    var courseList:[AnyObject]!
    override func viewDidLoad() {
        super.viewDidLoad()
self.tableView.backgroundColor = UIColor(red: 248/255, green: 250/255, blue: 254/255, alpha: 1)
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        self.title = "课件"
        self.tableView.register(CourseCell.self, forCellReuseIdentifier: "CourseCell")
        getCoursewareList()
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func getCoursewareList(){
        let parameters:NSDictionary = ["SessionID":AMNetworkMngTool.loginReturnModel!.sessionid!,"mod":"tware","page":"1"]
        AMNetworkMngTool.shared.AMNetwork_GetList(parameters) { (list) in
            DispatchQueue.main.async {
                if list != nil{
                    self.courseList = [AnyObject]()
                    for course in list!{
                        let model = CourseWareReturnJson.coursewareListModelWithDict(dict: course as? NSDictionary)
                        self.courseList.append(model!)
                    }
                    self.tableView.reloadData()
                }
                else{
                }
                
            }
            }
            
    }
    
    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if  self.courseList != nil {
            return self.courseList.count
        }
        return 0
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CourseCell", for: indexPath) as! CourseCell
        
        // Configure the cell...
        cell.selectionStyle = .none
        if self.courseList != nil{
            let model = self.courseList[indexPath.row] as! CourseWareReturnJson
            if model.thumb == nil || (model.thumb?.hasSuffix(".pdf"))!{
                cell.img.image = UIImage(named: "placeholder.png")
            }
            else{
                let imgUrlPre = "http://amicool.neusoft.edu.cn/Uploads/"
                let imgUrl = URL(string: (imgUrlPre + model.thumb!))
                let imgData = try! Data(contentsOf: imgUrl!)
                cell.img.image = UIImage(data: imgData)?.scaleImageToSize(CGSize(width: 80, height: 60))
            }
            cell.title.text = model.name
            cell.author.text = model.author
            cell.updateTime.text = model.update_time
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 110
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detaiController = CoursewareDetailController()
        detaiController.coursewareModel = self.courseList[indexPath.row] as! CourseWareReturnJson
        self.hidesBottomBarWhenPushed = true
        _ = navigationController?.pushViewController(detaiController, animated: true)
        self.hidesBottomBarWhenPushed = false
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
